#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "msg_queue_peer.h"

struct msgbuf
{
    long mtype;
    char mdata[256];
};

int main(int argc, char *argv[])
{
    struct msgbuf send_buf;
    send_buf.mtype = 1;

    while (1)
    {
        printf("please input:");
        
        // 使用 fgets 替代 fread
        if (fgets(send_buf.mdata, sizeof(send_buf.mdata), stdin) == NULL) {
            printf("读取输入失败\n");
            continue;
        }
        
        // 移除换行符
        size_t len = strlen(send_buf.mdata);
        if (len > 0 && send_buf.mdata[len-1] == '\n') {
            send_buf.mdata[len-1] = '\0';
        }
        
        if(strcmp(send_buf.mdata, "quit") == 0)
        {
            printf("quit\n");
            break;
        }
       
        int ret = msg_queue_send("topic", &send_buf, sizeof(send_buf), 0);
        if(ret < 0)
        {
            printf("msg_queue_send error, ret=%d\n", ret);
            return -1;
        }
        else {
            printf("消息发送成功\n");
        }
    }

    return 0;
}